package cn.com.company.main.client.ui;

import cn.com.company.main.client.entity.WebbomUserinfo;
import cn.com.company.main.client.service.GreetingService;
import cn.com.company.main.client.service.GreetingServiceAsync;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ModifyPwdDialog extends Dialog {
	private static ModifyPwdDialog instance;
	private WebbomUserinfo currentUser;
	private FormData formData = new FormData("-20");
	private TextField<String> oldPassword;
	private TextField<String> newPassword;
	private TextField<String> new1Password;
	private TextField<String> username;
	private TextField<String> loginName;

	public static ModifyPwdDialog getInstance(WebbomUserinfo entity) {
		if (instance == null) {
			instance = new ModifyPwdDialog(entity);
		}
		return instance;
	}

	public ModifyPwdDialog(WebbomUserinfo entity) {
		currentUser = entity;
		setModal(true);
		setBodyBorder(false);// 边框
		setButtons(Dialog.OKCANCEL);// 设置按钮
		setHideOnButtonClick(false);// 设置按钮点击后隐藏
		// setClosable(false);// 设置是否能关闭
		setResizable(false);// 设置改变大小
		setLayout(new FitLayout());
		createView();
		setButtonAlign(HorizontalAlignment.CENTER);
		this.getButtonById(Dialog.OK).setText("保存");
		this.getButtonById(Dialog.CANCEL).setText("取消");
		this.getButtonById(Dialog.OK).addSelectionListener(
				new SelectionListener<ButtonEvent>() {
					@Override
					public void componentSelected(ButtonEvent ce) {
						if (checkPassword()) {
							GreetingServiceAsync service = GWT
									.create(GreetingService.class);
							service.updateUserPassword(
									currentUser.getId() + "", oldPassword
											.getValue().trim(), newPassword
											.getValue().trim(),
									new AsyncCallback<String>() {
										@Override
										public void onSuccess(String result) {
											if (result != null
													&& result.equals("error")) {
												MessageBox.alert("错误",
														"您输入的原密码有误！", null);
											} else {
												MessageBox.alert("成功",
														"密码修改成功", null);
												hide();
												clearPanel();
											}
										}

										@Override
										public void onFailure(Throwable caught) {
											Info.display("错误", caught
													.getLocalizedMessage());
										}
									});
						}
					}
				});

		this.getButtonById(Dialog.CANCEL).addSelectionListener(
				new SelectionListener<ButtonEvent>() {
					@Override
					public void componentSelected(ButtonEvent ce) {
						hide();
						clearPanel();
					}
				});
	}

	public boolean checkPassword() {
		String oldPwd = oldPassword.getValue();
		String newPwd1 = newPassword.getValue();
		String newPwd2 = new1Password.getValue();
		if (oldPwd == null || "".equals(oldPwd.trim())) {
			MessageBox.alert("警告", "请输入原密码", null);
			return false;
		} else if (newPwd1 == null || "".equals(newPwd1.trim())) {
			MessageBox.alert("警告", "请输入新密码", null);
			return false;
		} else if (newPwd2 == null || "".equals(newPwd2.trim())) {
			MessageBox.alert("警告", "请输入确认密码", null);
			return false;
		} else if (!newPwd1.trim().equals(newPwd2.trim())) {
			MessageBox.alert("警告", "两次输入的密码不一致", null);
			return false;
		} 
//		else if (newPwd1.trim().length() < 6) {
//			MessageBox.alert("提示", "密码长度必须在6位以上（包含6位）", null);
//			return false;
//		} else if (isDigit(newPwd1.trim())) {
//			MessageBox.alert("提示", "密码不能全是数字，建议为6位以上字母数字混合", null);
//			return false;
//		}
		return true;
	}

	public boolean isDigit(String str) {
		for (char key : str.toCharArray()) {
			if (!Character.isDigit(key)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Method 显示
	 * 
	 */
	public void show() {
		setWidth(320);
		setHeight(250);
		setHeading("修改密码");// 设置标头
		super.show();

	}

	public void createView() {

		FormPanel formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setFrame(true);

		formPanel.setSize("100%", "100%");
		formPanel.setLayout(new FormLayout());

		username = new TextField<String>();
		username.setFieldLabel("用户名");
		username.setReadOnly(true);
		username.setStyleAttribute("backgroundImage", "none");
		username.setStyleAttribute("backgroundColor", "#BEBEBE");
		username.setWidth(200);
		username.setValue(currentUser.getUsername());
		formPanel.add(username, formData);

		loginName = new TextField<String>();
		loginName.setFieldLabel("登录名");
		loginName.setReadOnly(true);
		loginName.setStyleAttribute("backgroundImage", "none");
		loginName.setStyleAttribute("backgroundColor", "#BEBEBE");
		loginName.setWidth(200);
		loginName.setValue(currentUser.getLoginname());
		formPanel.add(loginName, formData);

		oldPassword = new TextField<String>();
		oldPassword.setFieldLabel("原密码");
		oldPassword.setPassword(true);
		oldPassword.setAllowBlank(true);
		oldPassword.setWidth(200);
		formPanel.add(oldPassword, formData);

		newPassword = new TextField<String>();
		newPassword.setFieldLabel("新密码");
		newPassword.setPassword(true);
		newPassword.setAllowBlank(true);
		newPassword.setWidth(200);
		formPanel.add(newPassword, formData);

		new1Password = new TextField<String>();
		new1Password.setFieldLabel("确认密码");
		new1Password.setPassword(true);
		new1Password.setAllowBlank(true);
		new1Password.setWidth(200);
		formPanel.add(new1Password, formData);

		add(formPanel);

	}

	public void clearPanel() {
		newPassword.setValue("");
		new1Password.setValue("");
		oldPassword.setValue("");
	}

}
